import 'package:casdon/core/network/i_api_client.dart';
import 'package:casdon/core/utils/json_parse_utils.dart';
import 'package:casdon/modules/home/data/api/auth_user_info_api.dart';
import 'package:casdon/modules/home/data/api/ciot_device_api.dart';
import 'package:casdon/modules/home/data/api/home_banner_list_api.dart';
import 'package:casdon/modules/home/data/api/splash_list_api.dart';
import 'package:casdon/modules/home/data/api/popup_list_api.dart';
import 'package:casdon/modules/home/data/repository/home_repository.dart';

class HomeRepositoryImpl extends HomeRepository {
  // 字段类型也是抽象接口
  final IApiClient _apiClient;

  HomeRepositoryImpl(this._apiClient);

  // 获取用户信息，用来涂鸦登录
  @override
  Future<ResponseAuthUserInfoApi?> getUserInfo() async {
    final res = await _apiClient.getTyped<ResponseAuthUserInfoApi>(
      '/auth-client/user/info',
      fromJsonT: (json) =>
          ResponseAuthUserInfoApi.fromJson(json as Map<String, dynamic>),
    );
    return res?.data;
  }

  // 获取用户的ciot设备
  @override
  Future<List<ResponseCiotDeviceApi>?> getCiotDeviceList() async {
    final res = await _apiClient.getTyped<List<ResponseCiotDeviceApi>>(
      '/client-user/user/device',
      fromJsonT: (json) => parseList(json, ResponseCiotDeviceApi.fromJson),
    );
    return res?.data;
  }

  // banner广告位
  @override
  Future<List<ResponseHomeBannerListApi>?> getHomeBannerList() async {
    final res = await _apiClient.getTyped<List<ResponseHomeBannerListApi>>(
      '/client-operation/position/getBannerList',
      query: {"type": 1},
      fromJsonT: (json) => parseList(json, ResponseHomeBannerListApi.fromJson),
    );
    return res?.data;
  }

  @override
  Future<List<ResponseSplashListApi>?> getSplashList() async {
    final res = await _apiClient.getTyped<List<ResponseSplashListApi>>(
      '/client-user/position/getSplashList',
      fromJsonT: (json) => parseList(json, ResponseSplashListApi.fromJson),
    );
    return res?.data;
  }

  @override
  Future<List<ResponsePopupListApi>?> getPopupList() async {
    final res = await _apiClient.getTyped<List<ResponsePopupListApi>>(
      '/client-user/position/getPopupList',
      fromJsonT: (json) => parseList(json, ResponsePopupListApi.fromJson),
    );
    return res?.data;
  }
}
